Introduction
The words “server-side javascript” would have been enticing for many web developers before the advent Node.js. The ability to write elegant JavaScript code to develop web servers and networking tools allows a developer to widen their exposure to the many technologies that comprise the web. Node.js was developed in 2009 in order to fill the need for an event-driven language with asynchronous design patterns to develop web applications. In a sense, Node.js brings the best of both worlds of an expressive scripting language and of the power of Unix networking tools. Stack Harbor has made it easy for you to deploy a virtual machine with a Node.js environment in just a few clicks.
Accessing Your Application
Once your Stack is built, you can log into your virtual machine remotely using SSH to verify that you have a working Node.js environment installed. If you don’t already know how to connect to your Stack using SSH, check out our “Getting Started with Your Stack” tutorial.
Once you’ve logged into your Stack, you can verify that you have a Node.js environment by running the following command:
nodejs --version
We have configured our one-click Node.js development stack to run Node.js v0.12.4. Node.js comes with a REPL which allows you to execute server-side JavaScript code line by line and see its output. Just execute the nodejs command on your command line prompt to access the node REPL.
A Tiny Web Server
What introductory tutorial wouldn’t be complete without a ubiquitous “Hello, World!” example. We’ll show you how to create a web server listening on a port on your Stack that will echo “Hello, World!” to the users browser. First, create JavaScript file using the following:
touch hello.js
Then, we can open the file using our favorite text editor and write some basic JavaScript that uses the Node.js api to respond to http requests. Just run nano hello.js in your terminal and insert the following code into the file:
var http = require('http');
// Every request is greeted with "Hello, World!"
var server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end("Hello, World!\n");
});
// Listen on port 5000 on your stack
server.listen(5000);
// Print out a nice message that will let you know if the server is running
console.log("Your server is running on your Stack's IP on port 5000!");
To save and exit the editor, hit CTRL-O, Enter/Return, CTRL-X. Finally, you can start your very simple web server written in Node.js by running
nodejs hello.js
This should log a message to your terminal that lets you know that your server is running. Now, if you point your browser to your Stack’s public IP address, you should be greeted with “Hello, World!” and be amazed that you wrote JavaScript code to develop your very own web server.
Final Words
Congratulations! You’re all set to begin developing web applications using one of the fastest-growing languages among web developers. Check out the excellent documentation written by the developers for more insight on the many things you can accomplish with Node.js. Further, check out our Community Section on more information and tutorials regarding development and server administration tasks. From all of us at Stack Harbor, ahoy!